<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="login_image">
        <img src="@/assets/logo.png" alt="">
      </div>
      <!-- 表单 -->
      <div class="login_form">
        <el-form
          :model="loginForm"
          :rules="loginFormRelus"
          ref='loginFormRgf'
          label-width="60px"
        >
          <el-form-item label="账号" prop='username'>
            <el-input v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop='password'>
            <el-input
              v-model="loginForm.password"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: '',
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      /**
       * required:是否必填
       * message：提示文字
       * trigger：触发动作
       * min、max：规定长度
       * pattern：正则表达式
       */
      loginFormRelus: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^[a-z0-9_-]{6,18}$/, message: '密码由6-18为字母数字下划线组成' }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      this.$refs.loginFormRgf.resetFields()
    },
    login () {
      this.$refs.loginFormRgf.validate(async valid => {
        if (!valid) return false
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        // console.log(this)
        // this.$message.success('登录成功')
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login_container{
  width: 100%;
  height: 100%;
  background: #2b4b6b;
  .login_box{
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background: white;
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    .login_image{
      width: 130px;
      height: 130px;
      background: #fff;
      border: 1px solid #ccc;
      box-shadow: 0 0 10px #eee;
      border-radius: 50%;
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      margin: -75px auto;
      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
      .el-form{
        width: 100%;
      }
    }
    .login_form{
      width: 100%;
      position: absolute;
      bottom: 30px;
      padding: 0 20px;
      box-sizing: border-box;
      .btns{
        display: flex;
        justify-content: center;
      }
    }
  }
}
</style>
